let app = getApp();
import { getAuthentic, createAntchain } from "../../api/index";
Page({
  data: {
    certName: '',
    certNo: '',
    userImg: '',
    orderId: ''
  },
  onLoad(option) {
    let userInfo = my.getStorageSync({ key: 'userInfo' }).data
    let curOrderId = my.getStorageSync({ key: 'orderId' }).data;
    this.setData({
      orderId: option.orderId || curOrderId,
      userImg: userInfo ? userInfo.avatar : ''
    })
    this.getInfo()
  },
  events: {
    onBack() {
      my.confirm({
        title: '提示',
        content: '当前订单未完成,是否继续',
        confirmButtonText: '是,继续',
        cancelButtonText: '否,回首页',
        success: res => {
          if (res.confirm) {
            my.reLaunch({ url: '../user/orderpay/orderpay?id=' + this.data.orderId })
          } else {
            my.switchTab({ url: '../index/index' })
          }
        },
      });
    },
  },
  onShow() {
    setTimeout(() => { my.hideLoading() }, 500);
  },
  getInfo() {
    getAuthentic(this.data.orderId).then(res => {
      this.setData({
        certName: res.cert_name,
        certNo: res.cert_no
      })
    })
  },
  // 智能合同
  smartContract() {
    const that = this
    createAntchain(that.data.orderId).then(res => {
      console.log('res', res)
      my.showLoading({ content: '正在跳转合同...' });
      function getSignUrl(aliSchema) {
        if (!aliSchema) { return '' }
        const querys = aliSchema.split('?')[1].split('&');
        const signUrlKeyValue = querys.find((item) => item.includes('query=')).replace('query=', '');
        const encodedSignUrl = decodeURIComponent(signUrlKeyValue).replace('signUrl=', '');
        return encodedSignUrl
      }
      const signUrl = getSignUrl(res.sign_url);
      my.navigateToMiniProgram({
        appId: '2021001152620480',
        path: `pages/signH5/index?signUrl=${signUrl}`,
        success: (res) => {
          console.log(JSON.stringify(res))
          my.hideLoading({
            page: that,
          });
        },
        fail: (res) => {
          my.hideLoading({
            page: that,
          });
          console.log(JSON.stringify(res))
        }
      });
    })
  },
  submitFlow() {
    this.smartContract()
  }
});
